package com.wyt.controller.outstorageController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.InstOrAgeAndDetailMapper;
import com.wyt.dao.InstOrAgeMapper;
import com.wyt.dao.OutstorAgeAndDetailMapper;
import com.wyt.dao.OutstorAgeMapper;
import com.wyt.entity.*;
import com.wyt.until.CommonResult;
import com.wyt.until.Result;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
//表名 t_instorage

/**
 * 获取所有已驳回订单
 * @param
 * @return
 */
//入库  状态为已驳回 状态码为6
@Controller
@RequestMapping("outStorageHistory")
public class OutstorageRejectController {
    @Autowired
    private OutstorAgeAndDetailMapper outstorAgeAndDetailMapper;
    @Autowired
    private OutstorAgeMapper outstorAgeMapper;

    @RequestMapping("selectAllInstOrAgeReject")
    @ResponseBody
    public TableLayui selectAllInstOrAge(String pname, String rname) {
        /*System.out.println(pname + "-----" + rname);*/
        List<InstOrAge> list = outstorAgeMapper.selectAllInstOrAgeReject(pname, rname);
        PageInfo<InstOrAge> pageInfo=new PageInfo<InstOrAge>(list);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;
//        CommonResult commonResult = new CommonResult(0, "", list);
//        return commonResult;

    }

    /**
     * 删除当前点击行的数据
     * @param
     * @return
     */
    //   删除单行数据
    @RequestMapping("delHistory")
    @ResponseBody
    public Result deleteHisetory(Integer pid) {
        int deleteHistory = outstorAgeMapper.deleteHistory(pid);
       /* System.out.println("==================="+pid+"=====================");*/
//        System.out.println("==================="+pid+"=====================");
        if (deleteHistory > 0) {
            Result result = new Result("1", "删除成功");
            return result;
        } else {
            Result result = new Result("2", "删除失败");
            return result;
        }
    }

    /**
     * 获取所有已入库订单
     * @param
     * @return
     */
    //查询入库 状态为已入库的  状态码为5
    @RequestMapping("querryAllInstOrAgeReject")
    @ResponseBody
    public TableLayui querryAllInstOrAgeReject(Integer page, Integer limit,String pname, String rname) {
      /*  System.out.println(pname + "-----" + rname);*/
        PageHelper.startPage(page,limit);
        List<InstOrAge> list = outstorAgeMapper.querryAllInstOrAgeReject(pname, rname);
        PageInfo<InstOrAge> pageInfo=new PageInfo<InstOrAge>(list);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;
//        CommonResult commonResult = new CommonResult(0, "", list);
//        return commonResult;

    }
    /**
     * 获取所有待入库订单
     * @param
     * @return
     */
    //查询入库  状态为待入库的需要通过或者驳回 状态码为3
    @RequestMapping("selectAllByStatus")
    @ResponseBody
    public TableLayui selectAllByStatus(Integer page, Integer limit, String pname) {
        PageHelper.startPage(page,limit);
        List<InstOrAge> list = outstorAgeMapper.selectAllByStatus(pname);
        PageInfo<InstOrAge> pageInfo=new PageInfo<InstOrAge>(list);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;
//        CommonResult commonResult = new CommonResult(0, "", list);
//        return commonResult;
    }

    @RequestMapping("/queryAllIns")
    @ResponseBody
    public CommonResult queryAllIns(String pname, String rname){
        List<InstOrAge> list = outstorAgeMapper.queryAllIns(pname, rname);
        CommonResult commonResult = new CommonResult(0, "", list);
        return commonResult;
    }
    /**
     * 通过入库申请   状态码 (3>>>5)
     * @param
     * @return
     */
    //入库审核通过单个申请
    @RequestMapping("passIns")
    @ResponseBody
    public Result passIns(@RequestParam("pid") Integer pid , HttpServletRequest request){
        HttpSession session = request.getSession();
        Employee employee = (Employee)session.getAttribute("login");
//        System.out.println(employee);
        List<OutstorAgeAndDetail> list =  outstorAgeAndDetailMapper.queryAllDetail(pid);
//        System.out.println("============================================");
//        System.out.println(list);
//        System.out.println(employee);
        int i = 0;
        int i1 = 0;
        System.out.println(list);
        System.out.println(pid);
        if (list.size()>0) {
            for (OutstorAgeAndDetail outstorAgeAndDetail: list) {
                //更改入库状态 存储入库人 入库时间
                i = outstorAgeMapper.passIns(employee.getdId(),employee.geteName(),pid);
                System.out.println(employee.getdId()+"====="+employee.geteName()+"======="+pid);
//
                //更新商品列表
                i1 = outstorAgeMapper.updateBook(outstorAgeAndDetail.getBookid(),outstorAgeAndDetail.getCount(),outstorAgeAndDetail.getPrice());
            }
        }
        Result result = null;
        System.out.println(i+"====="+i1);
        if (i>0 && i1>0){
            result = new Result("1","通过申请成功");
        }else {
            result = new Result("2","通过申请失败");
        }
        return result;
    }
    /**
     * 驳回入库申请   状态码 (3>>>6)
     * @param
     * @return
     */
    @RequestMapping("/updateTpStatusFour1")
    @ResponseBody
    public Result updateTpStatusFour(@RequestParam("desc") String desc, @RequestParam("pid") Integer pid,HttpServletRequest request){
        HttpSession session = request.getSession();
        //获取当前操作人的名字和id
        Employee employee = (Employee)session.getAttribute("login");
        int i = outstorAgeMapper.updateTpStatusFour(employee.getdId(),employee                                                      .geteName(),pid ,desc);
        Result result = null;
        if (i>0){
            result = new Result("1","驳回申请成功");
        }else {
            result = new Result("2","驳回申请失败");
        }
        return result;
    }

    /**
     * @Description:    批量删除入库历史
     * @Author:         wyt
     * @CreateDate:     2021/11/22 11:32
     * @UpdateRemark:
     */
    @RequestMapping("delAllHistory")
    @ResponseBody
    public CommonResult delAllHistory(Integer[] pidAll) {
        int i=0;
        for (Integer a:pidAll
             ) {
            i = outstorAgeMapper.deleteHistory(a);
        }
        CommonResult commonResult = new CommonResult();
        if(i>=1){
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
            return commonResult;
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
            return commonResult;
        }
    }
    /**
     * 入库审核批量通过
     * @param
     * @return
     */
    @RequestMapping("passAll")
    @ResponseBody
    public Result passAll(Integer[] pidAll,HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee employee = (Employee)session.getAttribute("login");
          int p = 0;
          int p1 = 0;
        for (Integer a :pidAll) {
            List<OutstorAgeAndDetail> list =  outstorAgeAndDetailMapper.queryAllDetail(a);
//            System.out.println("=========================================="+a);
            if (list.size() > 0) {
                for (OutstorAgeAndDetail outstorAgeAndDetail : list) {
                    //更改入库状态 存储入库人 入库时间
                    p = outstorAgeMapper.passIns(employee.getdId(), employee.geteName(),a);
                    //更新商品列表
                    p1 = outstorAgeMapper.updateBook(outstorAgeAndDetail.getBookid(), outstorAgeAndDetail.getCount(), outstorAgeAndDetail.getPrice());
                }
            }
        }
    Result result = null;
        if (p>0 && p1>0){
            result = new Result("1","通过申请成功");
        }else {
            result = new Result("2","通过申请失败");
        }
        return result;
    }
}